package cn.dylan.order.controller;


import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.LogininfoContext;
import cn.dylan.basic.util.PageList;
import cn.dylan.order.domain.OrderPetAcquisition;
import cn.dylan.order.dto.OrderDto;
import cn.dylan.order.query.OrderPetAcquisitionQuery;
import cn.dylan.order.service.IOrderPetAcquisiionService;
import cn.dylan.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {
    
    @Autowired
    private IOrderPetAcquisiionService orderPetAcquisitionService;

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<OrderPetAcquisition> QueryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LogininfoContext.getLogininfo(request);
            return orderPetAcquisitionService.queryPagePlus(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    @ApiOperation(value = "确认订单" )
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody OrderDto dto){
        try {
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统错误，请稍后重试");
        }
    }

    @DeleteMapping("/{id}")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("取消失败");
        }
    }
    
}
